<?php
/**
 * @政策类
 * 1、政策列表
 * 2、政策详情
 * 
 */
namespace app\policy\controller;
use app\common\controller\HomeCommon;
use think\Cache;
use think\Config;
use think\Request;

class Index extends HomeCommon
{
	public function __construct(){
		
		parent::__construct();
		
	
	}
	
	/**
	 * 获得政策的类别
	 */
	public function get_policy_type(){
		
		$where=['status'=>1];
		
		$field="type_name,identification";
		
		$model= model('Commondata');;
		$listobj=$model->get_list('policy_type',$field,$where);
		
		return json(['errcode'=>'0','msg'=>lang('Get success'),'data'=>$listobj]);
	}
  /**
     * 获得政策列表
    */
    public function get_policy(){
    	
    	
    	$where="  a.status=1  ";
    	$identification=request()->param('identification','','trim');
    	
    	
    	
    	$field="a.id,a.policy_name,a.accept_time,a.content,a.create_time,a.identification,a.type_id as type_name,a.province_id";
    	$obj=db('policy a');

   /****类型**/
    	if(!empty($identification)){
    		$model=\think\Loader::model('PolicyType');
    		$return=$model->get_type_id(['identification'=>$identification]);
    		if($return){
    			$where.="  and a.type_id=$return  ";
    		}
    		
    	} 
   /***关键字*/
    	$keywords=request()->param('keywords','','trim');
    	if(!empty($keywords)){
    		$where.=" and a.policy_name like '%$keywords%' ";
    	}
   /***行业*/
    	$industry_id=request()->param('industry_id',0,'intval');
    	if(!empty($industry_id)){
    		$obj=$obj->join('policy_industry b','a.id=b.policy_id');
    		$where.=" and b.status=1 and b.industry_id=$industry_id";
    	}
    	
   /***产业链*/
    	$chain_id=request()->param('chain_id',0,'intval');
    	if(!empty($chain_id)){
    		$obj=$obj->join('policy_chain c','a.id=c.policy_id');
    		$where.=" and c.status=1 and c.chain_id=$chain_id";
    	}
    	
   /***城市*/
    	$city_id=request()->param('city_id',0,'intval');
    	if(!empty($city_id)){
    		$where.=" and (a.province_id=$city_id or a.city_id=$city_id or a.area_id=$city_id ) ";
    	}
        /**政策类型**/
        $level=request()->param('level',0,'');
        if (!empty($level)){
            switch ($level) {
                //区域政策
                case 'qyzc':
                    $obj->where('a.level', '区域政策');
                    break;
                //省级政策
                case 'sjzc':
                    $obj->where('a.level', '省级政策');
                    break;
                //国家政策
                case 'gjzc':
                    $obj->where('a.level', '国家政策');
                    break;
                //经信委
                case 'jxw':
                    $obj->where('a.level', '经信委');
                    break;
                //科委    
                case 'kw':
                    $obj->where('a.level', '科委');
                    break;
                //金融局办
                case 'cy':
                    $obj->where('a.level', '金融局办');
                    break;
                //商委
                case 'js':
                    $obj->where('a.level', '商委');
                    break;
                default:
                    exit('未知排序字段');
                    break;
            }
        }
      // 设置分页数据
        $page=request()->param('page',0,'intval');
        $limit=request()->param('limit',15,'intval');
        Config::set('paginate.page',bcadd($page,1));
    	$listobj=$obj->field($field)->where($where)->paginate($limit)->toArray();




        //政策类型
        $type=db('policy_type')->where('status',1)->column('type_name','id');
        foreach ($listobj['data'] as &$value){
            // 政策类型
            if (!empty($value['type_name'])){
                $value['type_name']=explode(',',$value['type_name']);
                foreach ($value['type_name'] as $v){
                    if (empty($value['type_str'])){
                        $value['type_str']=[];
                    }
                    $value['type_str'][]=$type[$v];
                }
                $value['type_name']=$value['type_str'];
            }
            //城市
            $model=\think\Loader::model('Label');
            $value['city_name']=$model->city($value['province_id'])[0]['city_name'];
            //产业链
            if(!empty($chain_id)){
                $policy_chain_id=db('policy_chain')->where(['status'=>1,'policy_id'=>$value['id']])->min('chain_id');
                $value['chain_name']=db('chain')->where(['status'=>1,'id'=>$policy_chain_id])->value('chain_name');
            }
            unset($value['type_str'],$value['province_id'],$value['id']);
        }

    	return json(['errcode'=>'0','msg'=>lang('Get success'),'data'=>$listobj]);
    	
    	
    }
  /**
     * 获得政策详情
    */
    public function get_policy_info(){
    	
    	
    	$where['status']=['=',1];
    	
    	
    	$identification=request()->param('identification','','trim');


    	if(!empty($identification)){
    		$where['identification']=['=', $identification];
    		
    		
    		$cache=new Cache();
    		$result=$cache->get('policy_'.$identification);

    		
    		if($result&&1==2){
    			$info=unserialize($result);
    		}else{
    			$field="policy_name,accept_time,content,about_files,detail,create_time,identification";
    			$model= model('Commondata');
    			
    			$info=$model->get_detail('policy',$field,$where);
                if ($info) {
                    # code.
                    $info['about_files']=json_decode($info['about_files']);
                    $info['detail']=json_decode($info['detail']);
                }
               
    			$cache->set('policy_'.$identification, serialize($info),3600);
    		}
    		
    		
    		return json(['errcode'=>'0','msg'=>lang('Get success'),'data'=>$info]);
    		
    		
    	}else{
    		return json(['errcode'=>'-1','msg'=>lang('param error'),'data'=>[]]);
    	}
    	
    	
    	
    	
    	
    }

  /**
     * 根据城市获取政策
    */
    public function get_city_policy(){
        $field="province_id,city_id,policy_name,accept_time,content,create_time,identification";
        $obj=db('policy a')->where('a.status',1);

      /***产业链*/
        $chain_id=request()->param('chain_id',0,'intval');
        if(!empty($chain_id)){
            $obj->join('policy_chain c','a.id=c.policy_id');
            $obj->where('c.status',1)->where('c.chain_id',$chain_id);
        }
      /****城市*/
        $cids = '2,6,8,25,32';
        $obj->where('a.province_id','in',$cids);
        $listobj=$obj->field($field)->select();
        return json(['errcode'=>'0','msg'=>lang('Get success'),'data'=>$listobj]);
    }

  /**
     * 城市政策对比
    */
    public function city_diff_policy(Request $request)
    {
        $reqData['city_id']=$request->get('city_id',0,'intval');
        $reqData['other_city_id']=$request->get('other_city_id',0,'intval');
        $reqData['chain_id']=$request->get('chain_id',0,'intval');
        $reqData['type']=$request->get('type',0,'intval');
        $compare=$request->get('compare_type','','trim');

        $citys=array($reqData['city_id'],$reqData['other_city_id']);
        $result=[];

        foreach ($citys as $key=>$city){
            switch ($compare) {
                case "policy":
                    /****政策主表****/
                    $obj=db('policy a')
                        ->field('count(a.id) as total')
                        ->where('a.status',1)
                        ->where('a.province_id='.$city .' or a.city_id='.$city. ' or a.area_id='.$city);
                    /****政策产业链****/
                    if (!empty($reqData['chain_id'])){
                        $obj->join('policy_chain b','a.id=b.policy_id','LEFT')
                            ->where('b.status',1)
                            ->where('b.chain_id',$reqData['chain_id']);
                    }
                    /****政策类型****/
                    if (!empty($reqData['type'])){
                        $obj->join('policy_type c','a.type_id=c.id','LEFT')
                            ->where('c.status',1)
                            ->where('c.id',$reqData['type']);
                    }
                    break;
                case "company":
                    /****企业主表****/
                    $obj=db('companys a')
                        ->field('count(a.id) as total')
                        ->where('a.status',1)
                        ->where('a.province_id='.$city .' or a.city_id='.$city. ' or a.area_id='.$city);
                    /****企业产业链****/
                    if (!empty($reqData['chain_id'])){
                        $obj->join('companys_chain b','a.id=b.company_id','LEFT')
                            ->where('b.status',1)
                            ->where('b.chain_id',$reqData['chain_id']);
                    }
                    break;
                case "park":
                    /****园区主表****/
                    $obj=db('parks a')
                        ->field('count(a.id) as total')
                        ->where('a.status',1)
                        ->where('a.province_id='.$city .' or a.city_id='.$city. ' or a.area_id='.$city);
                    /****园区产业链****/
                    if (!empty($reqData['chain_id'])){
                        $obj->join('parks_chain b','a.id=b.park_id','LEFT')
                            ->where('b.status',1)
                            ->where('b.chain_id',$reqData['chain_id']);
                    }
                    break;
                default:
                    exit('未知错误');
            }
          /****城市信息****/
            $city_info=db('city')->where('id',$city)->field('id,city_name')->find();
            $city_info['total']=$obj->count();
            $result[$key]=$city_info;
        }
        return json(['errcode'=>'0','msg'=>lang('Get success'),'data'=>$result]);
    }
}
